/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.upn.sigemfi.gar.bc;

import edu.upn.sigemfi.gar.be.Tasador;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author Freddy
 */
public class TasadorDelegate {

    private TasadorFacadeLocal local;

    public TasadorDelegate(){
        local = lookupTasadorFacade();
    }

    private TasadorFacadeLocal lookupTasadorFacade() {
        try {
            Context c = new InitialContext();
            return (TasadorFacadeLocal) c.lookup("java:comp/env/TasadorFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    public void crear(Tasador tasador) throws Exception
    {
        try{
            local.create(tasador);
        }
        catch(Exception ex){
            local=lookupTasadorFacade();
            throw new Exception("Crear Tasador"+ex.getMessage());
        }
    }

    public void editar(Tasador tasador) throws Exception
    {
        try{
            local.edit(tasador);
        }
        catch(Exception ex){
            local=lookupTasadorFacade();
            throw new Exception("Actualizar Tasador"+ex.getMessage());
        }
    }

    public void eliminar(Tasador tasador) throws Exception
    {
        try{
            local.remove(tasador);
        }
        catch(Exception ex){
            local=lookupTasadorFacade();
            throw new Exception("Eliminar Tasador"+ex.getMessage());
        }
    }

    public List<Tasador> listar() throws Exception
    {
        try{
           return local.findAll();
        }
        catch(Exception ex){
            local=lookupTasadorFacade();
            throw new Exception("Listar Tasadores"+ex.getMessage());
        }
    }
}
